﻿package com._public._ui
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	/**
	 * ...
	 * @author 望月狼 (shch8.com)
	 * @example
	 * var but:ScButton=new ScButton
			but.label = "我是按钮"
			but.x = 150
			but.y = 250
			addChild(but)
		* @param 皮肤与样式 注：支持动态重绘
		* 如
		but.style.normalTxt=new TextFormat("宋体",12,0x333333,true)
		//正常文字样式//
		but.style.normalTxt = new TextFormat("宋体", 12, 0x333333, true)
		//鼠标滑过时的文字样式//
		but.style.overTxt = new TextFormat("宋体", 12, 0xcccccc, true)
		//鼠标按下的文字样式//
		but.style.downTxt = new TextFormat("宋体", 12, 0xffffff, true)
		//失效时的文字样式//
		but.style.enabledTxt = new TextFormat("宋体", 12, 0x333333, true)
		
		//正常的背景填充色//
		but.style.normalFill = 0xf8f8f8;
		//鼠标滑过的背景填充色//
		but.style.overFill = 0x333333;
		//鼠标按下的背景填充色//
		but.style.downFill = 0x444444;
		//失效的背景填充色//
		but.style.enabledFill = 0xeeeeee;
		
		//正常的按钮边框色//
		but.style.normalLine = 0xaaaaaa;
		//鼠标滑过时的按钮边框色//
		but.style.overLine = 0x000000;
		//鼠标按下时的按钮边框色//
		but.style.downLine = 0x000000;
		//鼠标失效时的按钮边框色//
		but.style.enabledLine = 0xffffff;
		
		//正常的按钮皮肤,影片剪辑或Sprite对象//
		but.style.normalFace;
		//鼠标滑过时的按钮皮肤,影片剪辑或Sprite对象//
		but.style.overFace;
		//鼠标按下时的按钮皮肤,影片剪辑或Sprite对象//
		but.style.downFace;
		//鼠标失效时的按钮皮肤,影片剪辑或Sprite对象//
		but.style.enabledFace;
		//圆角大小//
		but.style.ellipse = 3;
	 */
	public class ScButton extends Sprite
	{
		private var bg:Sprite
		private var selState:Sprite
		public var style:ParamSet=new ParamSet
		private const OVER:String = "over";
		private const DOWN:String = "down";
		private const NORMAL:String = "normal";
		private const ENABLED:String = "enabled";
		private const SELECTED:String = "sel";
		private const TXT:String = "Txt"
		private const FILL:String = "Fill"
		private const LINE:String = "Line"
		private const FACE:String = "Face"
		private const ALPHA:String = "Alpha"
		private var _state:String
		private var txt:TextField
		
		private var _label:String
		private var _wid:Number=0
		private var _hei:Number = 25
		private var _enabled:Boolean = true
		private var _selected:Boolean;
		private var _data:Object;//用户存放特殊数据
		public function ScButton(label:String="")
		{
			_label=label
			this.addEventListener(Event.ADDED_TO_STAGE,init)
		}
		private function init(e:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE,init)
			enabled=_enabled
		}
		public function get data():Object{return _data}
		public function set data(value:Object):void
		{
			_data=value
		}
		public function get selected():Boolean{return _selected}
		public function set selected(value:Boolean):void
		{
			if (_selected == value) return;
			_selected=value
			if(selState)selState.visible=value
		}
		public function get enabled():Boolean
		{
			return _enabled
		}
		public function set enabled(value:Boolean):void
		{
			_enabled = value
			mouseEnabled=value
			if (_enabled) {
				draw(NORMAL)
				this.addEventListener(MouseEvent.MOUSE_OVER, overFun)
			this.addEventListener(MouseEvent.MOUSE_OUT, outFun)
			this.addEventListener(MouseEvent.MOUSE_DOWN, downFun)
			this.addEventListener(MouseEvent.MOUSE_UP,overFun)
			}else {
				draw(ENABLED)
				this.removeEventListener(MouseEvent.MOUSE_OVER, overFun)
			this.removeEventListener(MouseEvent.MOUSE_OUT, outFun)
			this.removeEventListener(MouseEvent.MOUSE_DOWN, downFun)
			this.removeEventListener(MouseEvent.MOUSE_UP,overFun)
				}
		}
		public function set label(value:String):void { _label = value; if (txt) txt.text = value; draw(NORMAL);};
		public function get label():String {return _label};
		override public function get width():Number { return bg.width; }
		
		override public function set width(value:Number):void
		{
			_wid = value;
			draw();
		}
		override public function get height():Number { return _hei; }
		
		override public function set height(value:Number):void
		{
			_hei= value;
		}
		public function setSize(wid:Number, hei:Number):void {
			_wid = wid
			_hei = hei
			draw()
		}
		private function overFun(e:MouseEvent):void { draw(OVER) };
		private function outFun(e:MouseEvent):void { draw(NORMAL) };
		private function downFun(e:MouseEvent):void { draw(DOWN) };
		private function draw(state:String=NORMAL):void
		{
			_state = state
			if (bg == null) {
				selState = new Sprite
				selState.mouseEnabled = false
				selState.visible=selected
				addChild(selState)
				bg = new Sprite
				bg.mouseEnabled=false
				addChild(bg)
			}else {
				bg.graphics.clear();
				if(bg.numChildren>0){
				var getObj:DisplayObject=bg.getChildAt(0)
				if (getObj) bg.removeChild(getObj);
				}
				}
			
			if (txt == null) {
				txt = new TextField
				txt.mouseEnabled = false
				txt.autoSize="left"
				addChild(txt)
				}
			txt.text = _label
			txt.setTextFormat(param(TXT) as TextFormat)
			
			var setWid:Number = width
			if(setWid==0)setWid=txt.width+6
			if (param(FACE) == null) {
				bg.graphics.beginFill(uint(param(FILL)),Number(param(ALPHA)))
				bg.graphics.lineStyle(1,uint(param(LINE)),Number(param(ALPHA)),true)
				bg.graphics.drawRoundRect(0, 0,setWid, height, style.ellipse, style.ellipse)
				bg.graphics.endFill();
				}else {
					var spr:Sprite = param(FACE) as Sprite
					spr.width = setWid
					spr.height=height
					bg.addChild(spr)
					}
			txt.x = (setWid - txt.textWidth) / 2
			txt.y = (height - txt.height) / 2;
			selState.graphics.lineStyle(1,style.selLine,style.selAlpha,true)
			selState.graphics.drawRoundRect(-1, -1, setWid+2, height+2,style.ellipse,style.ellipse)
			selState.graphics.endFill();
		}
		private function param(value:String):Object
		{
			return style[_state+value]
		}
	}
	
}
import flash.display.Sprite;
import flash.text.TextFormat;
//设置对象
class ParamSet
{
		/*正常文字样式*/
		public var normalTxt:TextFormat = new TextFormat("宋体", 12, 0x333333)
		/*鼠标滑过时的文字样式*/
		public var overTxt:TextFormat = new TextFormat("宋体", 12, 0xcccccc)
		/*鼠标按下的文字样式*/
		public var downTxt:TextFormat = new TextFormat("宋体", 12, 0xffffff)
		/*失效时的文字样式*/
		public var enabledTxt:TextFormat = new TextFormat("宋体", 12, 0x333333)
		
		/*正常的背景填充色*/
		public var normalFill:uint = 0xf8f8f8;
		/*鼠标滑过的背景填充色*/
		public var overFill:uint = 0x333333;
		/*鼠标按下的背景填充色*/
		public var downFill:uint = 0x444444;
		/*失效的背景填充色*/
		public var enabledFill:uint = 0xeeeeee;
		
		/*正常的背景填充色*/
		public var normalAlpha:Number = 1;
		/*鼠标滑过的背景填充色*/
		public var overAlpha:Number = 1;
		/*鼠标按下的背景填充色*/
		public var downAlpha:Number = 1;
		/*失效的背景填充色*/
		public var enabledAlpha:Number = 1;
		
		/*正常的按钮边框色*/
		public var normalLine:uint = 0xaaaaaa;
		/*鼠标滑过时的按钮边框色*/
		public var overLine:uint = 0x000000;
		/*鼠标按下时的按钮边框色*/
		public var downLine:uint = 0x000000;
		/*鼠标失效时的按钮边框色*/
		public var enabledLine:uint = 0xffffff;
		/*选中时的按钮边框色*/
		public var selLine:uint = 0x0099ff;
		/*选中时的按钮边框透明度*/
		public var selAlpha:Number = 0.8;
		
		/*正常的按钮皮肤,影片剪辑或Sprite对象*/
		public var normalFace:Sprite;
		/*鼠标滑过时的按钮皮肤,影片剪辑或Sprite对象*/
		public var overFace:Sprite;
		/*鼠标按下时的按钮皮肤,影片剪辑或Sprite对象*/
		public var downFace:Sprite;
		/*鼠标失效时的按钮皮肤,影片剪辑或Sprite对象*/
		public var enabledFace:Sprite;
		/*选中时的按钮皮肤,影片剪辑或Sprite对象*/
		public var selFace:Sprite;
		
		/*圆角大小*/
		public var ellipse:uint = 3;
}
